<template>
  <div class="login_container">
   
    <div class="login_box">
      <div class="icon">
        <img src="../assets/logo.png" alt="">
      </div>
      <div class="text">
      <el-form ref="loginFormRef" label-width="0px" class="wenben" :model="loginForm" :rules="loginFormRules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" prefix-icon="iconfont icon-mima" type="password"></el-input>
        </el-form-item>
        <!-- 按钮区 -->
        <el-form-item class="btns"> 
          <el-button type="primary"  @click="login">登录</el-button>
          <el-button type="primary" @click="resetLoginForm">重置</el-button>
        </el-form-item>
        
          
        
      </el-form>

      </div>
    </div>
    
  </div>
</template>

<script>
export default {
  data(){
    return{
      // 这是登录表单的数据绑定对象
      loginForm:{
        username:'',
        password:''
      },
      // 这是登录表单的验证
      loginFormRules:{
        // 验证用户名是否合法
        username:[ { required: true, message: '请输入用户名', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }],
        // 验证密码是否合法
        password:[{required:true,message:"请输入密码",trigger:"blur"},
        {min: 6, max: 18 , message: '长度在 6 到 18 个字符', trigger: 'blur'}]
      }
    }
  },
  methods:{
    // 重置登录表单
    resetLoginForm(){
      // this获取当前实例,然后获取refs对象,获取提前为表单设置的ref,再调用重置表单方法
      this.$refs.loginFormRef.resetFields();
    },
    login(){
      this.$refs.loginFormRef.validate(async valid => {
        if(!valid) return;
        const {data: res} =await this.$http.post('login',this.loginForm)
        console.log(res);
        if(res.meta.status !==200){
          this.$message.error('登录失败');
        }else{
          this.$message.success('登录成功');
        }
        // 登录成功之后会有token值,将token值保存到客户端的sessionStorage
        // 项目里除了登录接口,其它接口必须登录后才能访问
        window.sessionStorage.setItem('token',res.data.token);
        // 并且跳转到后台主页
        this.$router.push('/home');
      })
    }
  }

}

</script>
<style lang='less' scoped>
  .login_container{
    background-color: #2b4b6b;
    height: 100%;
    
  }
  

  .login_box{
    width: 600px;
    height: 400px;
    background-color: white;
    border-radius:3px ;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
    }

    .icon{
      height: 130px;
      width: 130px;
      padding: 10px;
      border-radius: 50%;
      margin: -70px auto ;
      
      img {
        width: 100%;
        height:100%;
        border-radius: 50%;
        background-color: #eee;
       }
    }
    .btns{
      display: flex;
      justify-content: center;
    }
    .wenben{
      position: absolute;
      bottom: 0px;
      width: 100%;
      padding: 100px;
      box-sizing: border-box;
    }
    
</style>